(function() {
    'use strict';
    var checkedObject = {};
    /**
     * 公司职务列表  初始化设置
     * @type {Object}
     */
    var settings = {
        "sAjaxSource": window.globalHost + "/agent-sys/admin/role/list.do",
        "aoColumns": [{
            "mDataProp": function(obj) {
                var checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" >';
                if (checkedObject[obj.id]) {
                    checkboxItemStr = '<input id="checkbox-' + obj.id + '" type="checkbox" checked="checked">';
                }
                return '<div class="checkbox checkbox-danger" style="width:100%">' + checkboxItemStr + '<label for="checkbox-' + obj.id + '" style="width:80%"></label></div>'
            },
            "sTitle": "",
            "sDefaultContent": "",
            "sClass": "center"
        }, {
            "mDataProp":function (obj) {
                return "<a class='roleName' data-roleId="+obj.id+" data-departId="+obj.departId+" href='javascript:;'>"+obj.roleName+"</a>"
            },
            "sTitle": "职务名称",
            "sDefaultContent": ""
        }, {
            "mDataProp":"departName",
            "sTitle": "所属部门",
            "sDefaultContent": ""
        }, {
            "mDataProp": "createTime",
            "sTitle": "创建时间",
            "sDefaultContent": ""
        }],
        "fnServerParams": function(aoData) {
            return common.getSearchData("form.roleListSearch",aoData);
        }
    };
    // $("body").on("click",".allClick",function(ev){
    //     var list = $("body").find("input[type='checkbox']");
    //     list.each(function(i,I){
    //         $(I).trigger('click')
    //     })
    // });
    var options = $.extend(true, {}, _dataTableSettings, settings);
    /**
     * 实例化公司职务列表插件
     * @type {[type]}
     */
    var $currentDataTable=$(".dataTables-example");
    var oTable = $currentDataTable.dataTable(options);
    /**
     * 绑定数据列表中的多选框
     * @param  {[type]} ev){                     var checkItem [description]
     * @return {[type]}       [description]
     */
    $currentDataTable.on("change", ".checkbox", function(ev) {
        var checkItem = $(this).find("input[type='checkbox']")[0];
        var checkId = checkItem.id.replace('checkbox-', '');
        if (checkItem.checked) {
            checkedObject[checkId] = true;
        } else {
            delete checkedObject[checkId];
        }
        ev.stopPropagation();
    });
    var dialogDom="<div class='dialogPageCenter clearfix'>"+
        "<label class='col-sm-4' for='departName'>所属部门: </label>"+
        "<select name='departId' id='departId' class='col-sm-7'></select>"+
        "</div>"+
        "<div class='dialogPageCenter clearfix'>"+"" +
        "<label class='col-sm-4' for='roleName'>职务名称: </label>"+
        "<input class='col-sm-7' type='text' name='roleName' id='roleName' placeholder='请输入职务名称...'>"+
        "</div>";
    //单击添加职务按钮。弹框出现新增公司职务对话框 [新增公司职务弹框，common.openTempDialog 是一个公共的弹框。专门用户处理 "DOM模板弹框"]
    $("button.insertRoleButton").off('click').on('click', function(ev) {
        var sendData={};
        common.openTempDialog({
            title:"添加职务",
            area:['500px', '400px'],
            content:dialogDom,
            ajaxUrl:window.globalHost + "/agent-sys/admin/role/save.do",
            sendData:sendData,
            FailedMsg:"请输入部门和职务名称",
            contentOnLoad:function (layerDocument) {
                //创建公司职务下拉列表
                var $departNameSelect=layerDocument.find("#departId");
                buildSelect.departmentList($departNameSelect);
            },
            beforeSend:function (layerDocument) {
                //组装发送数据，添加职务,需要向后台传入的参数字段 departId:所属部门的id,roleName:职务名称
                var departIdVal=layerDocument.find("#departId").val();
                var $roleNameInput=layerDocument.find("#roleName");
                var roleNameVal=$roleNameInput.val();
                sendData.departId=departIdVal;
                sendData.roleName=roleNameVal;
                //返回发送数据的条件，公司部门和职务名称不能为空
                return !!departIdVal && !!roleNameVal;
            },
            resOk:function () {
                //添加成功更新列表数据
                oTable.fnClearTable();
            }
        });
        ev.stopPropagation();
    });
    //双击职务数据列表中的职务名称，则出现弹框修改职务名称
    $currentDataTable.off("dblclick").on("dblclick",".roleName",function (ev) {
        var sendData={};
        var $this=$(this);
        var selfRoleId=$this.attr("data-roleId");
        var selfDepartId=$this.attr("data-departId");
        var selfText=$this.html();
        common.openTempDialog({
            title:"修改职务名称",
            area:['400px', '400px'],
            content:dialogDom,
            ajaxUrl:window.globalHost + "/agent-sys/admin/role/update.do",
            sendData:sendData,
            FailedMsg:"请输入部门和职务名称",
            contentOnLoad:function (layerDocument) {
                //初始化文本框内容。使文本框内容和当前选中的一致
                var $departNameSelect=layerDocument.find("#departId");
                var $roleNameInput=layerDocument.find("#roleName");
                buildSelect.departmentList($departNameSelect,selfDepartId);
                $roleNameInput.val(selfText);
            },
            beforeSend:function (layerDocument) {
                //组装发送数据，添加职务,需要向后台传入的参数字段 departId:所属部门的id,id:当前自身的职务id,roleName:职务名称
                var departIdVal=layerDocument.find("#departId").val();
                var roleNameVal=layerDocument.find("#roleName").val();
                sendData.departId=departIdVal;
                sendData.id=selfRoleId;
                sendData.roleName=roleNameVal;
                //返回发送数据的条件，公司部门和职务名称不能为空
                return !!departIdVal && !!roleNameVal;
            },
            resOk:function () {
                //修改成功更新列表数据
                oTable.fnClearTable();
            }
        });
        ev.stopPropagation();
    });

    $("button.deleteRoleButton").on("click", function(ev) {
        common.deleteConfirm(batchDelete);
        ev.stopPropagation();
    });
    /**
     * 批量删除方法
     * @param  {[type]} data [description]
     * @return {[type]}      [description]
     */
    function batchDelete() {
        var ids = [];
        if ($.isEmptyObject(checkedObject)) {
            layer.alert("请选择需要删除的职务!");
            return;
        }
        $.each(checkedObject, function(i, I) {
            ids.push(i);
        });
        ids = ids.join();
        $.post(window.globalHost + '/agent-sys/admin/role/batchDelete.do', { ids: ids }).success(function(data) {
            var record = common.transformData(data);
            layer.alert(record.res.msg);
            /**
             * 删除成功之后，清除本地的checkbox缓存。
             * @type {Object}
             */
            checkedObject = {};
            /**
             * 批量删除成功之后，刷新列表
             */
            oTable.fnClearTable();
        });
    }
}).call(this);